#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#ifndef USB_CLASS_PER_INTERFACE
	#define USB_CLASS_PER_INTERFACE         0		//保留分类
#endif
#ifndef USB_CLASS_AUDIO
	#define USB_CLASS_AUDIO                 1		//音频设备
#endif
#ifndef USB_CLASS_COMM
	#define USB_CLASS_COMM                  2		//通信及通信类控制设备
#endif
#ifndef USB_CLASS_HID
	#define USB_CLASS_HID                   3		//人体工学设备
#endif
#ifndef USB_CLASS_PHYSICAL
	#define USB_CLASS_PHYSICAL              5		//物理设备
#endif
#ifndef USB_CLASS_STILL_IMAGE
	#define USB_CLASS_STILL_IMAGE           6		//图像设备
#endif
#ifndef USB_CLASS_PRINTER
	#define USB_CLASS_PRINTER               7		//打印设备
#endif
#ifndef USB_CLASS_MASS_STORAGE
	#define USB_CLASS_MASS_STORAGE          8		//大容量存储
#endif
#ifndef USB_CLASS_HUB
	#define USB_CLASS_HUB                   9		//集线器
#endif
#ifndef USB_CLASS_CDC_DATA
	#define USB_CLASS_CDC_DATA              0x0a		//通信类数据设备
#endif
#ifndef USB_CLASS_CSCID
	#define USB_CLASS_CSCID                 0x0b		//智能卡
#endif
#ifndef USB_CLASS_CONTENT_SEC
	#define USB_CLASS_CONTENT_SEC           0x0d		//内容安全性
#endif
#ifndef USB_CLASS_VIDEO
	#define USB_CLASS_VIDEO                 0x0e		//视频
#endif
#ifndef USB_CLASS_WIRELESS_CONTROLLER
	#define USB_CLASS_WIRELESS_CONTROLLER   0xe0		//无线控制器
#endif
#ifndef USB_CLASS_PERSONAL_HEALTHCARE
	#define USB_CLASS_PERSONAL_HEALTHCARE   0x0f		//个人医疗设备
#endif
#ifndef USB_CLASS_AUDIO_VIDEO
	#define USB_CLASS_AUDIO_VIDEO           0x10		//视频、音频设备
#endif
#ifndef USB_CLASS_BILLBOARD
	#define USB_CLASS_BILLBOARD             0x11		//公告牌设备
#endif
#ifndef USB_CLASS_USB_TYPE_C_BRIDGE
	#define USB_CLASS_USB_TYPE_C_BRIDGE     0x12		//usb type-c桥接类
#endif
#ifndef USB_CLASS_MISC
	#define USB_CLASS_MISC                  0xef		//混杂设备
#endif
#ifndef USB_CLASS_APP_SPEC
	#define USB_CLASS_APP_SPEC              0xfe		//应用相关
#endif
#ifndef USB_CLASS_VENDOR_SPEC
	#define USB_CLASS_VENDOR_SPEC           0xff		//自定义
#endif

#define UDEV_CONF "/proc/nfssec_devctl/usb_policy"
#define S_FILE "/proc/nfssec_devctl/devctl_switch"

struct usb_attr{
	unsigned int vid;
	unsigned int pid;
	unsigned int clsid;
};
struct dev_data_hdr{
	int usb_nu;
	int usb_wlist_nu;
	char data[0];
};

int parse_data(void *data){
	struct dev_data_hdr *hdr;
	int i;
	struct usb_attr *attr;
	void *start;

	hdr = (struct dev_data_hdr *)data;
	start = data + sizeof(struct dev_data_hdr);

	printf("all_usb=%d\n", hdr->usb_nu);
	for(i = 0; i < hdr->usb_nu; i++){
		attr = start + i * sizeof(struct usb_attr);
		printf("vid=%04x pid=%04x clsid=%04x\n", 
				attr->vid, 
				attr->pid,
				attr->clsid);
	}

	start = start + hdr->usb_nu * sizeof(struct usb_attr);
	printf("wlist_usb=%d\n", hdr->usb_wlist_nu);
	for(i = 0; i < hdr->usb_wlist_nu; i++){
		attr = start + i * sizeof(struct usb_attr);
		printf("vid=%04x pid=%04x clsid=%04x\n", 
				attr->vid, 
				attr->pid,
				attr->clsid);
	}

}

int func()
{
	int fd;
	char buf[1024] = { 0 };
	char buf2[4096] = { 0 };
	struct usb_attr *attr;

	buf[0] = '+';
	attr = (struct usb_attr *)(buf + 1);

	attr->vid = 0x0951;
	attr->pid = 0x1666;
	attr->clsid = 0x0008;
	
	//打开proc文件
	fd = open(UDEV_CONF, O_RDWR);
	if(fd < 0){
		printf("open failed\n");
		return -1;
	}

	//向内核写数据
	int n = write(fd, buf, 1+sizeof(struct usb_attr));
	printf("write n=%d\n", n);

	/*
	attr->vid = 0xaaaa;
	attr->pid = 0xbbbb;
	attr->clsid = 0x08;
	n = write(fd, buf, 1+sizeof(struct usb_attr));
	printf("write n=%d\n", n);

	buf[0] = '-';
	attr->vid = 0xaaaa;
	attr->pid = 0xbbbb;
	attr->clsid = 0x08;
	n = write(fd, buf, 1+sizeof(struct usb_attr));
	printf("write n=%d\n", n);
	//从内核读数据
	n = read(fd, buf2, 4096);
	printf("read n=%d\n", n);
	parse_data(buf2);

	buf[0] = '-';
	attr->vid = 0xaaaa;
	attr->pid = 0xbbbb;
	attr->clsid = 0x08;
	n = write(fd, buf, 1+sizeof(struct usb_attr));
	printf("write n=%d\n", n);
	*/

	memset(buf2, 0x0, 4096);
	n = read(fd, buf2, 4096);
	printf("read n=%d\n", n);
	parse_data(buf2);

	return 0;
}
enum dev_type{
	DEV_TYPE_USB = 0,			//USB类型
	DEV_TYPE_DEVICE,			//device类型
	DEV_TYPE_INTF,				//interface类型
	DEV_TYPE_MAX
};
struct dev_switch{
	int type;				//设备大类device usb interface
	int sub_type;				//子设备类型
	int open;				//1为功能生效，0为不生效
};
enum intf_type_eu{
	INTF_SERIAL = 1,			//串口
	INTF_PARALLEL,				//并口
	INTF_1394,				//1394接口
	INTF_PCMCIA,				//PCMCIA接口
	INTF_TYPEC,				//type-c接口
	INTF_HDMI,				//hdmi接口
	INTF_DISPLAYPORT,			//display接口
	INTF_MAX
};

//device类型子类
enum device_type_eu{
	DEVICE_BLUETOOTH = 1,			//蓝牙
	DEVICE_INFRARED,			//红外
	DEVICE_MODEM,				//modem设备
	DEVICE_STORAGE_DRIVE,			//存储驱动器sd tf卡
	DEVICE_SCSI_RAID,			//scsi raid驱动器
	DEVICE_WINCE,				//wince设备 安卓手机、pad等
	DEVICE_FLOPPY_DRIVE,			//软驱
	DEVICE_IMAGE,				//图像设备  摄像头、数码相机、扫描仪
	DEVICE_MULTI_SERIAL_CARD,		//多串口卡
	DEVICE_ETHERNET,			//以太网
	DEVICE_WIRELESS,			//无线
	DEVICE_PRINTER,				//打印机
	DEVICE_PRINTER_LOCAL,			//本地打印机
	DEVICE_CDROM,				//刻录机
	DEVICE_VOLUMESHADOW,			//卷影设备
	DEVICE_TAPE,				//磁带机
	DEVICE_MAX
};

int parse_func2(void *data, int n){
	struct dev_switch *item;
	int size = n/sizeof(struct dev_switch);
	int i;
	printf("size=%d of=%d", size, sizeof(struct dev_switch));

	item = data;
	for(i = 0; i < size; i++){
		printf("type=%d subtype=%d open=%d\n",
				item->type,
				item->sub_type,
				item->open);
		item++;
	}

	return 0;
}

int func2(void){
	int fd;
	char buf[4096]={ 0 };

	struct dev_switch set;
	set.type = DEV_TYPE_USB;
	set.sub_type = USB_CLASS_MASS_STORAGE;
	set.open = 1;

	fd = open(S_FILE, O_RDWR);
	if(fd<0){
		printf("open switch failed");
		return -1;
	}

	int n = write(fd, &set, sizeof(struct dev_switch));
	printf("write n=%d\n", n);

	n = read(fd, buf, 4096);
	printf("read n=%d\n", n);
	//parse_func2(buf, n);





	return 0;
}

int main(int argc, char **argv){
	func2();
	//func();


	return 0;
}
